function getList(){
  $.ajax({
    type: 'POST',
    url: '/content',
    xhrFields: {
      withCredentials: true
    },
    contentType: "application/x-www-form-urlencoded; charset=utf-8",
    success: function(response){
      if(response.status === '0'){
        showList(response.data);
        $('.header .logout').css('display', 'block');
        $('.reply').css('display', 'block');
      }else if( response.status === '-1'){

      } else if(response.status === '-2'){    //还未登录
        $('.header .aboutLogin').css('display', 'block');
        $('.nologin').css('display', 'block');
      }
    }
  })
}
getList();

//展示列表
function showList(list){
  $('.main .list').empty().css('display', 'none');
  $('.main .empty').css('display', 'none');
  if(list){
    $('.main .list').css('display', 'block');
    list.forEach(function(item, i){
      $('.main .list').append(`
        <div class="item">
          <div class="message">${item.msg}</div>
          <div class="username">${item.username}</div>
          <div class="time">${item.time}</div>
        </div>
      `);
    })
  }else {
    $('.main .empty').css('display', 'block');
  }
}

$('.reply button').click(function(){
  var content = $('#content').val();
  var vimg = $('#code').val();
  if(!content){
    alert('回复内容不能为空');
    return;
  } else if (!vimg){
    alert('验证码不能为空');
    return;
  }
  $.ajax({
    type: 'POST',
    url: '/content/reply',
    data: {
      vimg: vimg,
      msg: content
    },
    xhrFields: {
      withCredentials: true
    },
    contentType: "application/x-www-form-urlencoded; charset=utf-8",
    success: function(response){
      if(response.status === '0'){
        getList();
      }else if( response.status === '-1'){

      }
    }
  })
})


//退出
$('.logout a').click(function(){
  if(confirm("确认要退出？")){
    $.ajax({
      type: 'POST',
      url: '/login/logout',
      xhrFields: {
        withCredentials: true
      },
      contentType: "application/x-www-form-urlencoded; charset=utf-8",
      success: function(response){
        if(response.status === '0'){
          location.href = 'login.html';
        }else if( response.status === '-1'){

        }
      }
    })
  }

})
